﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Text;
using System.IO;
public partial class view : System.Web.UI.Page
{
    public string list = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            list=showMsg(Server.MapPath("guestbook.xml"));
        
        }

    }
    /// <summary>
    /// 调入xml文件，如果不存在，就创建一个
    /// </summary>
    /// <param name="file"></param>
    /// 
    private void LoadXml(string file)
    {
       XmlDocument xmlDoc = new XmlDocument();
        if (File.Exists(Server.MapPath(file)))
        {
            xmlDoc.Load(Server.MapPath(file));
        }
        else
        {
            XmlTextWriter xwriter = new XmlTextWriter(Server.MapPath(file), System.Text.Encoding.UTF8);
            xwriter.WriteStartDocument(true);
            //写入根元素
            xwriter.WriteStartElement("guestbook");
            xwriter.WriteEndElement();
            xwriter.Close();
        }
    } 
    private string showMsg(string file) 
    {
        StringBuilder s = new StringBuilder();
        if (!File.Exists(Server.MapPath(file)))
        {
            return "暂时无留言";
        }
        XmlReader xr = XmlReader.Create(file);
        xr.MoveToContent(); 
        s.Append("<table border='1' width='500' style='border-collapse:collapse;'>");
        while (xr.Read())
        {
         
            if (xr.NodeType == XmlNodeType.Element)
            {
                
                if  (xr.Name=="name")
                    s.Append("<tr><td>姓名</td><td>"+xr.ReadString()+"</td></tr>\n");
                else if(xr.Name=="add")
                    s.Append("<tr><td>地址</td><td>"+xr.ReadString()+"</td></tr>\n");
                else   if(xr.Name=="tel")
                    s.Append("<tr><td>电话</td><td>"+xr.ReadString()+"</td></tr>\n");
                else    if(xr.Name=="content")
                    s.Append("<tr><td>内容</td><td>"+xr.ReadString()+"</td></tr>\n");
               
            }
            
        
        }
        s.Append("</table>");
        xr.Close();
        
        return s.ToString();

     } 
}
